package com.ruoyi.knowledge.controller;

import java.util.List;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.knowledge.domain.KnowledgeBase;
import com.ruoyi.knowledge.service.IKnowledgeBaseService;

/**
 * 知识库管理Controller
 *
 * @author waky
 */
@RestController
@RequestMapping("/knowledge-base")
@Tag(name = "知识库管理")
public class KnowledgeBaseController extends BaseController {
    @Autowired
    private IKnowledgeBaseService knowledgeBaseService;

    /**
     * 查询知识库列表
     */
    @Operation(
            summary = "查询知识库列表",
            parameters = {
                    @Parameter(name = "knowledgeBase", description = "知识库信息"),
                    @Parameter(name = "search", description = "模糊查询关键字")
            })
    @GetMapping
    public TableDataInfo list(KnowledgeBase knowledgeBase,
                              @RequestParam(value = "search", required = false) String search) {
        startPage();
        // 如果有搜索关键字，设置到 name 和 description 中进行搜索
        if (search != null && !search.isEmpty()) {
            knowledgeBase.setName(search);
            knowledgeBase.setDescription(search);
        }
        List<KnowledgeBase> list = knowledgeBaseService.selectKnowledgeBaseList(knowledgeBase);
        return getDataTable(list);
    }

    /**
     * 获取知识库详细信息
     */
    @Operation(
            summary = "获取知识库详细信息",
            parameters = {
                    @Parameter(name = "id", description = "知识库ID")
            })
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(knowledgeBaseService.selectKnowledgeBaseById(id));
    }

    /**
     * 新增知识库
     */
    @Operation(
            summary = "新增知识库",
            parameters = {
                    @Parameter(name = "knowledgeBase", description = "知识库信息")
            })
    @Log(title = "知识库管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KnowledgeBase knowledgeBase) {
        String username = SecurityUtils.getUsername();
        knowledgeBase.setCreator(username);
        knowledgeBase.setModifier(username);
        int rows = knowledgeBaseService.insertKnowledgeBase(knowledgeBase);
        if (rows > 0) {
            return success(knowledgeBase.getId());
        }
        return error("知识库创建失败");
    }

    /**
     * 修改知识库
     */
    @Operation(
            summary = "修改知识库",
            parameters = {
                    @Parameter(name = "id", description = "知识库ID"),
                    @Parameter(name = "knowledgeBase", description = "知识库信息")
            })
    @Log(title = "知识库管理", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}")
    public AjaxResult edit(@PathVariable("id") Long id, @RequestBody KnowledgeBase knowledgeBase) {
        knowledgeBase.setId(id);
        knowledgeBase.setModifier(SecurityUtils.getUsername());
        return toAjax(knowledgeBaseService.updateKnowledgeBase(knowledgeBase));
    }

    /**
     * 删除知识库
     */
    @Operation(
            summary = "删除知识库",
            parameters = {
                    @Parameter(name = "id", description = "知识库ID")
            })
    @Log(title = "知识库管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id) {
        return toAjax(knowledgeBaseService.deleteKnowledgeBaseById(id));
    }
}
